import { Component } from '@angular/core';
// 导入  Article 模块
import { Article } from '../article/article/article.module';

@Component({
  selector: 'app-reddit',
  templateUrl: './reddit.component.html',
  styleUrls: ['./reddit.component.scss']
})
export class RedditComponent {
  // 定义  articles  的类型为 Article的数组
  articles:Article[]
  constructor(){
    // 添加参数
    this.articles = [
      new Article('Angular 2','http://angular.io',3),
      new Article('HTML','http://fullstack.io',2),
      new Article('CSS','http://angular.io',1)
    ]
  }
  // 定义一个添加文章的方法
    addArticle(title:HTMLInputElement,link:HTMLInputElement):Boolean{
      title.value =''
      link.value = ''
      this.articles.push(new Article(title.value,link.value,0))
      return false
    }

    // 定义一个根据评分排序的方法
    sortedArticles():Article[]{
      return this.articles.sort((a:Article,b:Article)=>b.votes - a.votes)
    }
}
